clear all

 
set more off

******* bring qualtrics data into stata format

local importData = 1

if (`importData' == 1) {


local numSessions1 = 3
local numSessions2 = 11
local numSessions3 = 26



forvalues session = 1/`numSessions1' {
	insheet using "data/raw/JMPlab_Session_`session'.csv", names 
	gen id = _n
	drop if id == 1 
	replace id = id - 1
	replace id = `session'*100 + id
	gen session = `session'
	destring, replace
	drop feedback 
	capture: drop finalpayinduced
	gen osu = 0
	save "data/raw_`session'.dta", replace
	clear
}

forvalues session = 1/`numSessions2' {
	insheet using "data/raw/JMPlabExtended_Session_`session'.csv", names
	gen id = _n
	drop if id == 1 
	replace id = id - 1
	local sessionPlus = `session' + `numSessions1'
	replace id = `sessionPlus'*100 + id
	gen session = `sessionPlus'
	destring, replace
	drop feedback
	gen osu = 0
	capture: drop selofferbig seldecisionbig finalpayinduced
	save "data/raw_`sessionPlus'.dta", replace
	clear
}


forvalues session = 1/`numSessions3' {
	insheet using "data/raw/JMPlab_OSU_3_Session_`session'.csv", names
	gen id = _n
	drop if id == 1 
	replace id = id - 1
	local sessionPlus = `session' + `numSessions1'+ `numSessions2'
	replace id = `sessionPlus'*100 + id
	gen session = `sessionPlus'
	destring, replace
	drop feedback
	gen osu = 1
	capture: drop selofferbig seldecisionbig finalpayinduced
	save "data/raw_`sessionPlus'.dta", replace
	clear
}


local numSessions1 = 3
local numSessions2 = 11
local numSessions3 = 26
local numSessions4 = 3
local numSessions5 = 5

forvalues session = 1/`numSessions4' {
	insheet using "data/raw/JMPlab_Next_Stanford_Session_`session'.csv", names
	gen id = _n
	drop if id == 1 
	replace id = id - 1
	local sessionPlus = `session' + `numSessions1'+ `numSessions2' +`numSessions3'
	replace id = `sessionPlus'*100 + id
	gen session = `sessionPlus'
	destring, replace
	drop feedback
	gen osu = 1 		// this affects the coding of the switching points! Needs to be 1 except for the first batch of stanford sessions!
	capture: drop selofferbig seldecisionbig finalpayinduced
	save "data/raw_`sessionPlus'.dta", replace
	clear
}

local numSessions5 = 5
forvalues session = 1/`numSessions5' {
	insheet using "data/raw/JMPlab_MIchigan_Session_`session'.csv", names
	gen id = _n
	drop if id == 1 
	replace id = id - 1
	local sessionPlus = `session' + `numSessions1'+ `numSessions2' +`numSessions3' +`numSessions4'
	replace id = `sessionPlus'*100 + id
	gen session = `sessionPlus'
	destring, replace
	drop feedback
	gen osu = 1			// this affects the coding of the switching points! Needs to be 1 except for the first batch of stanford sessions!
	capture: drop selofferbig seldecisionbig finalpayinduced
	save "data/raw_`sessionPlus'.dta", replace
	clear
}

}

local numSessions1 = 3
local numSessions2 = 11
local numSessions3 = 26
local numSessions4 = 3
local numSessions5 = 5

local numSessionsTotal = `numSessions1' + `numSessions2'  + `numSessions3' + `numSessions4' + `numSessions5'



use "data/raw_1.dta"
forvalues session = 2/`numSessionsTotal' {
	append using "data/raw_`session'.dta", force
	di `session'
}




// keep observations for which payment info at the end of the survey was displayed (i.e. drop computers that were started, but no subject sat on)
keep if q81 == 1



****** renaming

replace male = . if male == 3



replace confusing1 = 4 - confusing1
replace confusing2 = 4 - confusing2

rename attnpart2 attnPart2
rename mathuse mathUse




gen iqD1 = 	d1 	== 3 // Set iqDx equal to 1 if the corresponding question was answered correctly
gen iqD2 = 	d2 	== 4 
gen iqD3 =	d3 	== 3  
gen iqD4 = 	d4 	== 7 
gen iqD5 = 	d5 	== 8 
gen iqD6 = 	d6 	== 6 
gen iqD7 = 	d7 	== 5 
gen iqD8 = 	d8 	== 4 
gen iqD9 = 	d9 	== 1 
gen iqD10 = d10 == 2 
gen iqD11 = d11 == 5 
gen iqD12 = d12 == 6 

gen iqE1 = 	e1 	== 7
gen iqE2 = 	e2 	== 6 
gen iqE3 =	e3 	== 8  
gen iqE4 = 	e4 	== 2 
gen iqE5 = 	e5 	== 1 
gen iqE6 = 	e6 	== 5 
gen iqE7 = 	e7 	== 1 
gen iqE8 = 	e8 	== 6 
gen iqE9 = 	e9 	== 3 
gen iqE10 = e10 == 2 
gen iqE11 = e11 == 4 
gen iqE12 = e12 == 5 


gen iq = 0
gen iqD = 0
gen iqE = 0
forvalues i = 1 / 12 {
	replace iq = iq + iqD`i' + iqE`i'
	replace iqD = iqD + iqD`i'
	replace iqE = iqE + iqE`i'
}

su iq, d
gen iqHigh = iq > r(p50)
gen iqQ1 = iq <= r(p25)
gen iqQ2 = iq > r(p25) & iq <= r(p50)
gen iqQ3 = iq > r(p50) & iq <= r(p75)
gen iqQ4 = iq > r(p75)






	
gen crt1ok = crt1 == 0.05
gen crt2ok = crt2 == 5
gen crt3ok = crt3 == 47
gen crt4ok = crt4 == 29
gen crt5ok = crt5 == 20
gen crt6ok = crt6 == 96

gen crtScore = crt1ok + crt2ok + crt3ok + crt4ok + crt5ok + crt6ok

su crtS, d
gen crtHigh = crtS > r(p50)



replace spending = 25 if spending == 1
replace spending = 75 if spending == 2
replace spending = 125 if spending == 3
replace spending = 175 if spending == 4
replace spending = 250 if spending == 5
replace spending = 350 if spending == 6
replace spending = 450 if spending == 7
replace spending = 625 if spending == 8
replace spending = 875 if spending == 9
replace spending = 1250 if spending == 10
replace spending = 1750 if spending == 11
replace spending = 2500 if spending == 12
replace spending = 3500 if spending == 13

gen logSpending = log(spending)


replace political = political - 3



gen emailGiven = email ~= "" 
gen logFaminc = log(faminc)

gen afram = 	ethnicity == 1
gen indian = 	ethnicity == 6
gen eastAsian = ethnicity == 2
gen caucasian = ethnicity == 3
gen hispanic = 	ethnicity == 4
gen ethnOther = ethnicity == 5

gen artsHumanities = 	major == 1
gen science = 			major == 2
gen socScience = 		major == 4
gen engineering = 		major == 3
gen busEc = 			major == 5 

gen graduate = yearofstud >= 5


gen highInc = incamount == "high"
label define highInc 0 "low incentives" 1 "high incentives"
label values highInc highInc


gen infoTreatment = 1 - control
gen treatIndex = .
replace treatIndex = 1 if highInc == 1 & infoTreatment == 1
replace treatIndex = 2 if highInc == 0 & infoTreatment == 1
replace treatIndex = 3 if highInc == 1 & infoTreatment == 0
replace treatIndex = 4 if highInc == 0 & infoTreatment == 0

label de treatIndex 1 "Inc High, Info Yes" 2 "Inc Low, Info Yes" 3 "Inc High, Info No" 4 "Inc Low, Info No"
label val treatIndex treatIndex

replace age = age + 17

replace culture = 2 if culture > 2
gen cultureInEntomophagy = culture - 1


rename v8 startDate
rename v9 endDate
gen double startTime = clock(startDate, "YMDhms")
gen double endTime = clock(endDate, "YMDhms")
format startTime %tc
gen hour = hh(startTime)
replace hour = hour + 3 if osu == 1 // times are recorded in CA time
gen minute = mm(startTime)
gen sec = ss(startTime)







gen time_start_hour 	= regexs(1) if(regexm(startDate, "([0-9]+):[0-9][0-9]$"))
gen time_start_min 		= regexs(1) if(regexm(startDate, "([0-9][0-9])$"))
destring time_start_hour time_start_min, replace





save "data/dataRaw.dta", replace



********************************************************************************************
****** Part 1 ******************************************************************************
********************************************************************************************

clear
use "data/dataRaw.dta"

// information choice sessions 1 - 3
gen videoChoiceTry = infochoice == 1
replace videoChoiceTry = q258 == 1 if infochoice == . & osu == 0
replace videoChoiceTry = . if control == 1 & osu == 1
gen videoChoiceTryCont = infochcont_1


replace videoChoiceTryCont = q262_1 if infochcont_1 == .
gen videoWatchedTry = videodisplayed
replace vidinflu = . if vidinflu == 3
replace vidinflu = vidinflu - 1 if vidinflu >= 4
rename videxpec_1 videxpecTry
rename videxpec_5 videxpecNotTry

replace videxpecTry  = 2 if videxpecTry == 1
replace videxpecNotTry = 2 if videxpecNotTry == 1
replace videxpecTry  = 4 - videxpecTry
replace videxpecNotTry = 4 - videxpecNotTry



// information choice sessions 4 - 9

drop pro1 pro2 pro3 con1 con2 con3 oth1 oth2 oth3 

gen pro1 = q211_2_1 == 1
gen pro2 = q211_2_2 == 1 
gen pro3 = q211_2_3 == 1
gen con1 = q211_3_1 == 1
gen con2 = q211_3_2 == 1
gen con3 = q211_3_3 == 1
gen oth1 = q211_4_1 == 1
gen oth2 = q211_4_2 == 1
gen oth3 = q211_4_3 == 1


replace pro1 = clips_2_1 == 1 if osu == 1
replace pro2 = clips_2_2 == 1 if osu == 1 
replace pro3 = clips_2_3 == 1 if osu == 1
replace con1 = clips_3_1 == 1 if osu == 1
replace con2 = clips_3_2 == 1 if osu == 1
replace con3 = clips_3_3 == 1 if osu == 1
replace oth1 = clips_4_1 == 1 if osu == 1
replace oth2 = clips_4_2 == 1 if osu == 1
replace oth3 = clips_4_3 == 1 if osu == 1


gen numPro = pro1 + pro2 + pro3
gen numCon = con1 + con2 + con3
gen numOth = oth1 + oth2 + oth3

replace numPro = . if osu == 0
replace numCon = . if osu == 0
replace numOth = . if osu == 0
replace numPro = . if osu == 1 & control == 1
replace numCon = . if osu == 1 & control == 1
replace numOth = . if osu == 1 & control == 1

gen positiveIndex = numPro - numCon

gen numTotal = numPro + numOth + numCon




replace culture = 3 if culture == 2
replace culture = 2 if culture == 4

gen westernFoodCulture = 2 - foodhabits

gen insectBefore = insectbefo - 1

replace pet = . if pet == 3
replace pet = 2 - pet




****** get switching points



forvalues species = 1/5 {
	forvalues line = 1/21 {
		rename mplante_`line'`species' mplAnte`species'_`line'
		rename mplpost_`line'`species' mplPost`species'_`line'
		capture: rename mplbig_`line'`species' mplBig`species'_`line'
	}
}



reshape long mplAnte1_ mplAnte2_ mplAnte3_ mplAnte4_ mplAnte5_ ///
	mplPost1_ mplPost2_ mplPost3_ mplPost4_ mplPost5_ ///
	mplBig1_ mplBig2_ mplBig3_ mplBig4_ , i(id) j(line)
sort id line

foreach var in mplAnte1 mplAnte2 mplAnte3 mplAnte4 mplAnte5 mplPost1 mplPost2 mplPost3 mplPost4 mplPost5 mplBig1 mplBig2 mplBig3 mplBig4 {

	gen line_left = line
	gen line_right = line
	replace line_left = . if `var' == 2
	replace line_right = . if `var' == 1
	
	by id: egen switch_low = min(line_left)
	by id: egen switch_high = max(line_right)
	
	gen value_`var' = .
	

	// middle of interval
	replace value_`var' = 0 		if switch_low == 1 & osu == 0 // arbitrary value for low WTA
	replace value_`var' = 0.5 		if switch_low == 2 & osu == 0
	replace value_`var' = 1.5 		if switch_low == 3 & osu == 0
	replace value_`var' = 3 		if switch_low == 4 & osu == 0
	replace value_`var' = 5 		if switch_low == 5 & osu == 0
	replace value_`var' = 7 		if switch_low == 6 & osu == 0
	replace value_`var' = 9 		if switch_low == 7 & osu == 0
	replace value_`var' = 11.25 	if switch_low == 8 & osu == 0
	replace value_`var' = 13.75 	if switch_low == 9 & osu == 0
	replace value_`var' = 16.25 	if switch_low == 10 & osu == 0
	replace value_`var' = 18.75 	if switch_low == 11 & osu == 0
	replace value_`var' = 21.25 	if switch_low == 12 & osu == 0
	replace value_`var' = 23.75 	if switch_low == 13 & osu == 0
	replace value_`var' = 26.25 	if switch_low == 14 & osu == 0
	replace value_`var' = 28.75 	if switch_low == 15 & osu == 0
	replace value_`var' = 31.5 		if switch_low == 16 & osu == 0
	replace value_`var' = 34.5 		if switch_low == 17 & osu == 0
	replace value_`var' = 37.5 		if switch_low == 18 & osu == 0
	replace value_`var' = 41.5 		if switch_low == 19 & osu == 0
	replace value_`var' = 47 		if switch_low == 20 & osu == 0
	replace value_`var' = 55 		if switch_low == 21 & osu == 0
	replace value_`var' = 60 		if switch_low == .  & osu == 0 // arbitrary value for high WTA

	// middle of interval
	replace value_`var' = 0 		if switch_low == 1 & osu == 1 // arbitrary value for low WTA
	replace value_`var' = 0.5 		if switch_low == 2 & osu == 1
	replace value_`var' = 1.5 		if switch_low == 3 & osu == 1
	replace value_`var' = 2.5 		if switch_low == 4 & osu == 1
	replace value_`var' = 3.5 		if switch_low == 5 & osu == 1
	replace value_`var' = 5 		if switch_low == 6 & osu == 1
	replace value_`var' = 7 		if switch_low == 7 & osu == 1
	replace value_`var' = 9 		if switch_low == 8 & osu == 1
	replace value_`var' = 11.25 	if switch_low == 9 & osu == 1
	replace value_`var' = 13.75 	if switch_low == 10 & osu == 1
	replace value_`var' = 16.25 	if switch_low == 11 & osu == 1
	replace value_`var' = 18.75 	if switch_low == 12 & osu == 1
	replace value_`var' = 21.25 	if switch_low == 13 & osu == 1
	replace value_`var' = 23.75 	if switch_low == 14 & osu == 1
	replace value_`var' = 26.25 	if switch_low == 15 & osu == 1
	replace value_`var' = 28.75 	if switch_low == 16 & osu == 1
	replace value_`var' = 31.5 		if switch_low == 17 & osu == 1
	replace value_`var' = 34.5 		if switch_low == 18 & osu == 1
	replace value_`var' = 37.5 		if switch_low == 19 & osu == 1
	replace value_`var' = 41.5 		if switch_low == 20 & osu == 1
	replace value_`var' = 47 		if switch_low == 21 & osu == 1
	replace value_`var' = 55 		if switch_low == 22 & osu == 1
	replace value_`var' = 60 		if switch_low == .  & osu == 1 // arbitrary value for high WTA

	
	gen switch_`var' = switch_high

	drop line_left
 	drop line_right
	drop switch_high
	drop switch_low	
}	

keep if line == 1
drop line


gen diff1 = value_mplPost1 - value_mplAnte1
gen diff2 = value_mplPost2 - value_mplAnte2
gen diff3 = value_mplPost3 - value_mplAnte3
gen diff4 = value_mplPost4 - value_mplAnte4
gen diff5 = value_mplPost5 - value_mplAnte5

forvalues i = 1 / 5 {
	rename value_mplAnte`i' vAnte`i'
	rename value_mplPost`i' vPost`i'
}

forvalues i = 1 / 4 {
	local iPlus =  `i' + 5
	rename condhigh`i'  condhigh`iPlus'
	rename condlow`i'  condlow`iPlus'
	rename randhigh`i'  randhigh`iPlus'
	rename randlow`i'  randlow`iPlus'
	rename value_mplBig`i'  vBig`iPlus'
	rename switch_mplBig`i'  switch_mplBig`iPlus'
	forvalues j = 1 / 5 {
		rename canbig`j'up`i' canbig`j'up`iPlus'
		rename canbig`j'dow`i' canbig`j'dow`iPlus'
	}
}


rename disgust_7 disgust_6
rename disgust_8 disgust_7
rename disgust_9 disgust_8
rename disgust_10 disgust_9





// timing variables



rename tcancsmall_31 tCancel1
rename tcancsmall_32 tCancel2
rename tcancsmall_33 tCancel3
rename tcancsmall_34 tCancel4
rename tcancsmall_35 tCancel5

rename tcancbig_31 tCancel6
rename tcancbig_32 tCancel7
rename tcancbig_33 tCancel8
rename tcancbig_34 tCancel9

replace tCancel6 = tcancbigd_31 if canceltreatmentorder == "down"
replace tCancel7 = tcancbigd_32 if canceltreatmentorder == "down"
replace tCancel8 = tcancbigd_33 if canceltreatmentorder == "down"
replace tCancel9 = tcancbigd_34 if canceltreatmentorder == "down"







reshape long diff vAnte vPost switch_mplAnte switch_mplPost switch_mplBig binarydec ///
	insectexpe_ tried_ estothall estothhigh estothlow canplushig canpluslow ///
	canownmin cancownhal condhigh condlow randhigh randlow vBig disgust_ ///
	cancel1up cancel2up cancel3up cancel4up cancel5up ///
	cancel1dow cancel2dow cancel3dow cancel4dow cancel5dow ///
	canbig1up canbig2up canbig3up canbig4up canbig5up ///
	canbig1dow canbig2dow canbig3dow canbig4dow canbig5dow ///
	tCancel /// 	
	switch, i(id) j(species)

gen absDiff = abs(diff)



// define interval bounds for own WTA


gen upperVante = .
gen upperVpost = .
gen lowerVante = .
gen lowerVpost = .

replace upperVante = 0 		if vAnte == 0 
replace upperVante = 1 		if vAnte == 0.5 
replace upperVante = 2		if vAnte == 1.5 
replace upperVante = 3 		if vAnte == 2.5 
replace upperVante = 4 		if vAnte == 3 
replace upperVante = 4 		if vAnte == 3.5 
replace upperVante = 6 		if vAnte == 5 
replace upperVante = 8 		if vAnte == 7 
replace upperVante = 10 	if vAnte == 9 
replace upperVante = 12.5	if vAnte == 11.25
replace upperVante = 15 	if vAnte == 13.75
replace upperVante = 17.5	if vAnte == 16.25
replace upperVante = 20 	if vAnte == 18.75
replace upperVante = 22.5	if vAnte == 21.25
replace upperVante = 25 	if vAnte == 23.75
replace upperVante = 27.5	if vAnte == 26.25
replace upperVante = 30 	if vAnte == 28.75
replace upperVante = 33 	if vAnte == 31.5 
replace upperVante = 36 	if vAnte == 34.5 
replace upperVante = 39 	if vAnte == 37.5 
replace upperVante = 44 	if vAnte == 41.5 
replace upperVante = 50 	if vAnte == 47 
replace upperVante = 60 	if vAnte == 55 
replace upperVante = .		if vAnte == 60 

replace upperVpost = 0 		if vPost == 0 
replace upperVpost = 1 		if vPost == 0.5 
replace upperVpost = 2		if vPost == 1.5 
replace upperVpost = 3 		if vPost == 2.5 
replace upperVpost = 3 		if vPost == 3 
replace upperVpost = 4 		if vPost == 3.5 
replace upperVpost = 6 		if vPost == 5 
replace upperVpost = 8 		if vPost == 7 
replace upperVpost = 10 	if vPost == 9 
replace upperVpost = 12.5	if vPost == 11.25
replace upperVpost = 15 	if vPost == 13.75
replace upperVpost = 17.5	if vPost == 16.25
replace upperVpost = 20 	if vPost == 18.75
replace upperVpost = 22.5	if vPost == 21.25
replace upperVpost = 25 	if vPost == 23.75
replace upperVpost = 27.5	if vPost == 26.25
replace upperVpost = 30 	if vPost == 28.75
replace upperVpost = 33 	if vPost == 31.5 
replace upperVpost = 36 	if vPost == 34.5 
replace upperVpost = 39 	if vPost == 37.5 
replace upperVpost = 44 	if vPost == 41.5 
replace upperVpost = 50 	if vPost == 47 
replace upperVpost = 60 	if vPost == 55 
replace upperVpost = .		if vPost == 60 


replace lowerVante = .		if vAnte == 0 
replace lowerVante = 0 		if vAnte == 0.5 
replace lowerVante = 1 		if vAnte == 1.5 
replace lowerVante = 2		if vAnte == 2.5 
replace lowerVante = 2 		if vAnte == 3 
replace lowerVante = 3 		if vAnte == 3.5 
replace lowerVante = 4 		if vAnte == 5 
replace lowerVante = 6 		if vAnte == 7 
replace lowerVante = 8 		if vAnte == 9 
replace lowerVante = 10 	if vAnte == 11.25
replace lowerVante = 12.5	if vAnte == 13.75
replace lowerVante = 15 	if vAnte == 16.25
replace lowerVante = 17.5	if vAnte == 18.75
replace lowerVante = 20 	if vAnte == 21.25
replace lowerVante = 22.5	if vAnte == 23.75
replace lowerVante = 25 	if vAnte == 26.25
replace lowerVante = 27.5	if vAnte == 28.75
replace lowerVante = 30 	if vAnte == 31.5 
replace lowerVante = 33 	if vAnte == 34.5 
replace lowerVante = 36 	if vAnte == 37.5 
replace lowerVante = 39 	if vAnte == 41.5 
replace lowerVante = 44 	if vAnte == 47 
replace lowerVante = 50 	if vAnte == 55 
replace lowerVante = 60 	if vAnte == 60 


replace lowerVpost = .		if vPost == 0 
replace lowerVpost = 0 		if vPost == 0.5 
replace lowerVpost = 1 		if vPost == 1.5 
replace lowerVpost = 2		if vPost == 2.5 
replace lowerVpost = 2		if vPost == 3 
replace lowerVpost = 3 		if vPost == 3.5 
replace lowerVpost = 4 		if vPost == 5 
replace lowerVpost = 6 		if vPost == 7 
replace lowerVpost = 8 		if vPost == 9 
replace lowerVpost = 10 	if vPost == 11.25
replace lowerVpost = 12.5	if vPost == 13.75
replace lowerVpost = 15 	if vPost == 16.25
replace lowerVpost = 17.5	if vPost == 18.75
replace lowerVpost = 20 	if vPost == 21.25
replace lowerVpost = 22.5	if vPost == 23.75
replace lowerVpost = 25 	if vPost == 26.25
replace lowerVpost = 27.5	if vPost == 28.75
replace lowerVpost = 30 	if vPost == 31.5 
replace lowerVpost = 33 	if vPost == 34.5 
replace lowerVpost = 36 	if vPost == 37.5 
replace lowerVpost = 39 	if vPost == 41.5 
replace lowerVpost = 44 	if vPost == 47 
replace lowerVpost = 50 	if vPost == 55 
replace lowerVpost = 60 	if vPost == 60 




sort id
egen tagId = tag(id)

label de species 1 "2 house crickets" 2 "5 superworms" 3 "3 silkworm puppae" 4 "2 mole crickets" 5 "2 field crickets"  6 "3 small scorpions" 7 "1 large scorpion" 8 "1 male rhino beetle" 9 "1 giant water bug"
label values species species

replace insectexpe = insectexpe - 3

replace binarydec = 2 - binarydec // 0 = reject, 1 = accept
gen tried = 2 - tried_ // 1 = yes, 0 = no (have you tried some of the food items during this study)


replace disgust = 6 if disgust == 8
replace disgust = disgust - 2
rename disgust_ disgust

replace vidchange = vidchange - 3


rename cancownhal canownhal
replace canplushig = canplushig - 1
replace canpluslow = canpluslow - 1
replace canownmin = canownmin - 1
replace canownhal = canownhal - 1

replace condhigh = condhigh - 1
replace condlow = condlow - 1
replace randhigh = randhigh - 1
replace randlow = randlow - 1

replace vBig = 0 		if switch_mplBig == 1
replace vBig = 1.25 	if switch_mplBig == 2
replace vBig = 3.75 	if switch_mplBig == 3
replace vBig = 6.25 	if switch_mplBig == 4
replace vBig = 8.75 	if switch_mplBig == 5
replace vBig = 12.5 	if switch_mplBig == 6
replace vBig = 17.5 	if switch_mplBig == 7
replace vBig = 22.5 	if switch_mplBig == 8
replace vBig = 27.5 	if switch_mplBig == 9
replace vBig = 35 		if switch_mplBig == 10
replace vBig = 45 		if switch_mplBig == 11
replace vBig = 55 		if switch_mplBig == 12
replace vBig = 65 		if switch_mplBig == 13
replace vBig = 75 		if switch_mplBig == 14
replace vBig = 85 		if switch_mplBig == 15
replace vBig = 95 		if switch_mplBig == 16
replace vBig = 112.5 	if switch_mplBig == 17
replace vBig = 137.5 	if switch_mplBig == 18
replace vBig = 175 		if switch_mplBig == 19
replace vBig = 225 		if switch_mplBig == 20
replace vBig = 275 		if switch_mplBig == 21
replace vBig = 300 		if switch_mplBig == . & session > 3 & species >= 6

replace vBig = . if session <= 3 | species <= 5

gen batch = . 
replace batch = 1 if session <= 3
replace batch = 2 if session > 3 & session <= 9
replace batch = 3 if session > 9


gen knowBefore = 1 - (knowbefore == 3)
replace knowBefore = . if session <= 3


gen repulsive = vAnte > 0
replace repulsive = . if species == 5 & batch == 1
gen topTrunc = vAnte == 60
replace topTrunc = . if species == 5 & batch == 1

gen acceptHigh = vAnte <= 30


gen worm = species == 2 | species == 5
gen cricket = species == 1 | species == 4


forvalues i = 1 / 5 {
	gen cancel`i' = .
	replace cancel`i' = cancel`i'up if canceltreatmentordernum == 1 & species <= 5
	replace cancel`i' = cancel`i'dow if canceltreatmentordernum == 0 & species <= 5	
	replace cancel`i' = canbig`i'up if canceltreatmentordernum == 1 & species >= 6
	replace cancel`i' = canbig`i'dow if canceltreatmentordernum == 0 & species >= 6
	replace cancel`i' = cancel`i' - 1
}

rename canceltreatmentordernum cancelOrderUp






gen noSwitchDown = cancel1 <= cancel2 & cancel2 <= cancel3 & cancel3 <= cancel4 & cancel4 <= cancel5
gen oneSwitchUp = cancel1 ==  0 & cancel5 == 1

gen noSwitchUp = cancel1 >= cancel2 & cancel2 >= cancel3 & cancel3 >= cancel4 & cancel4 >= cancel5
gen oneSwitchDown = cancel1 ==  1 & cancel5 == 0

gen singleSwitchUp = noSwitchDown * oneSwitchUp
gen singleSwitchDown = noSwitchUp * oneSwitchDown

su singleSwitchUp
su singleSwitchUp if species >= 6

su singleSwitchDown
su singleSwitchDown if species >= 6



sort id
by id: egen singleSwitchUpSumSmall_i = sum(singleSwitchUp) if species <= 5
by id: egen singleSwitchUpSumBig_i = sum(singleSwitchUp) if species >= 6 & species <= 9
by id: egen singleSwitchUpSumAll_i = sum(singleSwitchUp)
by id: egen singleSwitchUpSumSmall = max(singleSwitchUpSumSmall_i)
by id: egen singleSwitchUpSumBig = max(singleSwitchUpSumBig_i)
by id: egen singleSwitchUpSumAll = max(singleSwitchUpSumAll_i)

by id: egen singleSwitchDownSumSmall_i = sum(singleSwitchDown) if species <= 5
by id: egen singleSwitchDownSumBig_i = sum(singleSwitchDown) if species >= 6 & species <= 9
by id: egen singleSwitchDownSumAll_i = sum(singleSwitchDown)
by id: egen singleSwitchDownSumSmall = max(singleSwitchDownSumSmall_i)
by id: egen singleSwitchDownSumBig = max(singleSwitchDownSumBig_i)
by id: egen singleSwitchDownSumAll = max(singleSwitchDownSumAll_i)


gen switchUpType = singleSwitchUpSumAll > 0 & singleSwitchDownSumAll == 0
gen switchDownType = singleSwitchUpSumAll == 0 & singleSwitchDownSumAll > 0


// define interval bounds for cancelling decisions for those who have a single switch.

gen lowerCancel = .
gen upperCancel = .

replace lowerCancel = . 	if cancel1 == 1						& species >= 6 		& switchUpType == 1
replace lowerCancel = 5 	if cancel2 == 1 & cancel1 == 0		& species >= 6 		& switchUpType == 1
replace lowerCancel = 30 	if cancel3 == 1 & cancel2 == 0		& species >= 6 		& switchUpType == 1
replace lowerCancel = 100 	if cancel4 == 1 & cancel3 == 0		& species >= 6 		& switchUpType == 1
replace lowerCancel = 200 	if cancel5 == 1 & cancel4 == 0		& species >= 6 		& switchUpType == 1
replace lowerCancel = 300 	if 				  cancel5 == 0		& species >= 6 		& switchUpType == 1


replace upperCancel = 5 	if lowerCancel == .					& species >= 6 		& switchUpType == 1
replace upperCancel = 30 	if lowerCancel == 5					& species >= 6 		& switchUpType == 1
replace upperCancel = 100	if lowerCancel == 30				& species >= 6 		& switchUpType == 1
replace upperCancel = 200	if lowerCancel == 100				& species >= 6 		& switchUpType == 1
replace upperCancel = 300	if lowerCancel == 200				& species >= 6 		& switchUpType == 1
replace upperCancel = .		if lowerCancel == 300				& species >= 6 		& switchUpType == 1


replace lowerCancel = 300 	if 				  cancel5 == 1		& species >= 6 		& switchDownType == 1
replace lowerCancel = 200 	if cancel5 == 0 & cancel4 == 1		& species >= 6 		& switchDownType == 1
replace lowerCancel = 100 	if cancel4 == 0 & cancel3 == 1		& species >= 6 		& switchDownType == 1
replace lowerCancel = 30 	if cancel3 == 0 & cancel2 == 1		& species >= 6 		& switchDownType == 1
replace lowerCancel = 5 	if cancel2 == 0 & cancel1 == 1		& species >= 6 		& switchDownType == 1
replace lowerCancel = . 	if cancel1 == 0						& species >= 6 		& switchDownType == 1

replace upperCancel = .		if lowerCancel == 300				& species >= 6 		& switchDownType == 1
replace upperCancel = 300	if lowerCancel == 200				& species >= 6 		& switchDownType == 1
replace upperCancel = 200	if lowerCancel == 100				& species >= 6 		& switchDownType == 1
replace upperCancel = 100	if lowerCancel == 30				& species >= 6 		& switchDownType == 1
replace upperCancel = 30	if lowerCancel == 5					& species >= 6 		& switchDownType == 1
replace upperCancel = 5		if lowerCancel == .					& species >= 6 		& switchDownType == 1

replace lowerCancel = . 	if cancel1 == 1						& species <= 5 		& switchUpType == 1
replace lowerCancel = 2		if cancel2 == 1 & cancel1 == 0		& species <= 5 		& switchUpType == 1
replace lowerCancel = 10 	if cancel3 == 1 & cancel2 == 0		& species <= 5 		& switchUpType == 1
replace lowerCancel = 20 	if cancel4 == 1 & cancel3 == 0		& species <= 5 		& switchUpType == 1
replace lowerCancel = 50 	if cancel5 == 1 & cancel4 == 0		& species <= 5 		& switchUpType == 1
replace lowerCancel = 80 	if 				  cancel5 == 0		& species <= 5 		& switchUpType == 1


replace upperCancel = 2		if lowerCancel == .					& species <= 5 		& switchUpType == 1
replace upperCancel = 10 	if lowerCancel == 2					& species <= 5 		& switchUpType == 1
replace upperCancel = 20	if lowerCancel == 10				& species <= 5 		& switchUpType == 1
replace upperCancel = 50	if lowerCancel == 20				& species <= 5 		& switchUpType == 1
replace upperCancel = 80	if lowerCancel == 50				& species <= 5 		& switchUpType == 1
replace upperCancel = .		if lowerCancel == 80				& species <= 5 		& switchUpType == 1


replace lowerCancel = 80  	if 				  cancel5 == 1		& species <= 5 		& switchDownType == 1
replace lowerCancel = 50  	if cancel5 == 0 & cancel4 == 1		& species <= 5 		& switchDownType == 1
replace lowerCancel = 20  	if cancel4 == 0 & cancel3 == 1		& species <= 5 		& switchDownType == 1
replace lowerCancel = 10 	if cancel3 == 0 & cancel2 == 1		& species <= 5 		& switchDownType == 1
replace lowerCancel = 2		if cancel2 == 0 & cancel1 == 1		& species <= 5 		& switchDownType == 1
replace lowerCancel = . 	if cancel1 == 0						& species <= 5 		& switchDownType == 1

replace upperCancel = .		if lowerCancel == 80				& species <= 5 		& switchDownType == 1
replace upperCancel = 80	if lowerCancel == 50				& species <= 5 		& switchDownType == 1
replace upperCancel = 50	if lowerCancel == 20				& species <= 5 		& switchDownType == 1
replace upperCancel = 20	if lowerCancel == 10				& species <= 5 		& switchDownType == 1
replace upperCancel = 10	if lowerCancel == 2					& species <= 5 		& switchDownType == 1
replace upperCancel = 2		if lowerCancel == .					& species <= 5 		& switchDownType == 1








// add statistics concerning estimates of others



preserve
gen est1 = estothhigh
gen est0 = estothlow
keep if species <= 5
reshape long est, i(id species) j(othHigh)

gen othDiffEst = .
gen othDiffSd = .
egen id2 = group(id)
su id2
local n = r(max)
forvalues i = 1 / `n' {
	capture: reg est othHigh if id2 == `i'
	capture: replace othDiffEst = _b[othHigh] if id2 == `i'
	capture: replace othDiffSd = _se[othHigh] if id2 == `i' 
}
keep if species == 1 
keep id othDiffEst othDiffSd
collapse othDiffEst othDiffSd, by(id)
save "data/othDiff.dta", replace
restore

capture: drop _merge
merge n:1 id using "data/othDiff.dta"
drop _merge


gen othDiffWeight = 1 / (othDiffSd^2)
su othDiffEst,d
gen othDiffExtreme = othDiffEst < r(p10) | othDiffEst > r(p90)
gen othDiffEstSq = othDiffEst^2
gen othDiffEstP = 2*(1 - normal(abs(othDiffEst/othDiffSd)))

rename vidinflu vidinfluence



// clean data


replace control = 0 if session <= 15
gen interactionIncControl = highInc * control
gen knowInsects = insectBef == 1 | pet == 1


replace vPost = . if species == 5 & session <= 14 	// drop large field crickets for ex post 
replace vAnte = . if species == 5 & session <= 14	// drop the 2 small mealworms for stanford sessions that were replaced by the field crickets. 
replace binarydec = . if species == 5 & session <= 14




// flip choices of the girl who mixed up left and right for the small insects (OSU session 9)
replace vAnte = 60 if v1 == "R_9SGNdcevgmh1IMZ" & species <= 5
replace vPost = 60 if v1 == "R_9SGNdcevgmh1IMZ" & species <= 5
replace binarydec = 0 if v1 == "R_9SGNdcevgmh1IMZ" & species <= 5


capture: drop binarydec1 binarydec2 binarydec3 binarydec4 binarydec5



gen university = 1 if session < 15 | (session >= 41 & session <= 43)
replace university = 2 if session >= 15 & session <= 40
replace university = 3 if session >= 44 & session <= 49




	
label var male "Male"
label var age "Age"
label var afram "African-American"
label var caucasian "Caucasian"
label var eastAsian "East Asian"
label var hispanic "Hispanic"
label var indian "Indian"
label var ethnOther "Other"
label var logSpending "log(monthly spending)"
label var logFaminc "log(annual family income)"
label var yearofstud "Year of study"
label var graduate "Graduate student"
label var artsHumanities "Arts and humanities"
label var science "Science"
label var socScience "Social Science"
label var engineering "Engineering"
label var busEc "Business or economics"
label var political  "Political orientation"
label var iq "Raven's score"
label var crtS "CRT score"
label var insectBefore "Has intentionally eaten insects before"
label var culture "Grown up in culture that practices entomophagy"
label var westernFood "Grown up eating mostly western foods"
label var pet "Had a pet that fed on store-bought insects"
label var knowbefore "Knew that this study concerns insect eating"


label de ethnicity 1 "afrAm" 2 "asian" 3 "caucasian" 4 "hispanic" 5 "other" 6 "indian"
label val ethnicity ethnicity

label de major 1 "artHum" 2 "science" 3 "engineering" 4 "socSci" 5 "busEc"
label val major major

save "data/dataInsects.dta", replace





/*

**** use no-Info treatments to measure risk aversion

keep if infoCond == 2


sort id
by id: egen riskPref = mean(valueCont)
by id: egen noisiness = sd(valueCont)

egen tag2 = tag(id)
keep if tag2 == 1

capture: drop _merge
save "data/riskPrefs.dta", replace

clear
use "data/dataInsects.dta"
capture drop _merge
merge m:1 id using "data/riskPrefs.dta"
forvalues i = 1 / 5 {
	drop insectexpe_`i'
}
drop tag2
save "data/dataInsects.dta", replace

*/









